#include "stdafx.h"
#include "UCArrowButton.h"
#include "../ucslib.h"

CUCArrowButton::CUCArrowButton(void)
{
	lstrcpy(m_szClsName,_T("UCArrowButton"));
}

CUCArrowButton::~CUCArrowButton(void)
{

}


LPCTSTR CUCArrowButton::GetClass() const
{
	return m_szClsName;
}


void CUCArrowButton::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
	if (_tcsicmp(pstrName, _T("ArrowNormalImage")) == 0)
	{
		SetArrowNormalImage(pstrValue);
	}	
	else if (_tcsicmp(pstrName, _T("ArrowPushedImage")) == 0)
	{
		SetArrowPushedImage(pstrValue);
	}
	else
	{
		CUCButton::SetAttribute(pstrName,pstrValue);
	}
}

int CUCArrowButton::SetArrowPushedImage(LPCTSTR lpszImage)
{
	m_sArrowPushedImage = lpszImage;
	return 0;
}

int CUCArrowButton::SetArrowNormalImage(LPCTSTR lpszImage)
{
	m_sArrowNormalImage = lpszImage;
	return 0;
}

int CUCArrowButton::GetArrowRect(RECT &rc)
{
	rc = m_rcItem;
	rc.left = rc.right - (rc.bottom - rc.top);
	return 0;
}


void CUCArrowButton::DrawText(HDC hDC,const RECT& rcPaint)
{
	if(!m_sText.IsEmpty() )
	{
		int nLinks = 0;
		RECT rc = m_rcItem;
		rc.left += m_rcTextPadding.left;
		rc.right -= m_rcTextPadding.right;
		rc.top += m_rcTextPadding.top;
		rc.bottom -= m_rcTextPadding.bottom;

		DWORD clrColor = IsEnabled()?m_dwTextColor:m_dwDisabledTextColor;

		if (m_uButtonState & UISTATE_PUSHED)
		{
			clrColor = GetPushedTextColor();
		}
		else if(m_uButtonState & UISTATE_HOT)
		{
			clrColor = GetHotTextColor();
		}
		else if(m_uButtonState & UISTATE_FOCUSED)
		{
			clrColor = GetFocusedTextColor();
		}


		if( m_bShowHtml )
		{
			CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, m_sText, clrColor, NULL, NULL, nLinks, m_uTextStyle);
		}
		else
		{
			CRenderEngine::DrawText(hDC, m_pManager, rc, m_sText, clrColor, m_iFont, m_uTextStyle);
		}
	}
}

void CUCArrowButton::DoPaint(HDC hDC, const RECT& rcPaint)
{
	if( !::IntersectRect(&m_rcPaint, &rcPaint, &m_rcItem) ) 
	{
		return;
	}

	PaintBkColor(hDC);

	DrawImage(hDC,rcPaint);
	if (!m_sArrowNormalImage.IsEmpty())
	{
		const TImageInfo *info = CUIC::GetImageInfo(m_pManager,(LPCTSTR)m_sArrowNormalImage);
		if (info)
		{
			RECT rc;
			GetArrowRect(rc);
			rc.top = (rc.bottom + rc.top - info->nY)/2;
			rc.bottom = rc.top + info->nY;
			rc.left = (rc.right  + rc.left - info->nX)/2 ;
			rc.right = rc.left + info->nX;

			LPCTSTR lpszImage = (LPCTSTR)m_sArrowNormalImage;
			if (m_uButtonState & UISTATE_PUSHED)
			{
				lpszImage = (LPCTSTR)m_sArrowPushedImage;
			}


			CRenderEngine::DrawImageString(hDC, m_pManager, rc, m_rcPaint,lpszImage , NULL);
		}
	} 

	DrawText(hDC,rcPaint);
	PaintBorder(hDC);
}


void CUCArrowButton::DoEvent(TEventUI& event)
{
	CButtonUI::DoEvent(event);
}

